package indi.yudidi.ioc.how2java.pojo;


import indi.yudidi.ioc.how2java.annotation.MyAutowired;

import java.lang.reflect.Field;

public class BaseInject {

    public BaseInject(){
        inject(this);//this:代表要创建的子类对象
    }

    /**
     * 执行子类构造器时,都会首先执行inject函数,把@MyAutowired标记的属性
     * @param auto
     */
    private void inject(BaseInject auto){
        Field[] fields=auto.getClass().getDeclaredFields();
        for(Field field : fields ){
            MyAutowired myAutowired=null;
            if((myAutowired=field.getAnnotation(MyAutowired.class))!=null){
                field.setAccessible(true);
                try{
                    field.set(this,myAutowired.paramValue() );
                }catch(Exception e){
                    System.out.println("错误");
                }
            }
        }
    }
}
